package com.un.ebs.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.sys.domain.DictionaryArea;
import com.un.ebs.sys.mapper.DictionaryAreaMapper;
import com.un.ebs.sys.service.DictionaryAreaService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liubo
 * @since 2020-05-11
 */
@Service
public class DictionaryAreaServiceImpl extends ServiceImpl<DictionaryAreaMapper, DictionaryArea> implements DictionaryAreaService {

    @Cacheable(cacheNames = "dictionary_area", key = "#parentId")
    @Override
    public List<DictionaryArea> query(Integer parentId) {
        LambdaQueryWrapper<DictionaryArea> lq = Wrappers.lambdaQuery();
        if (parentId == null) {
            lq.eq(DictionaryArea::getParentId, 0);
        } else {
            lq.eq(DictionaryArea::getParentId, parentId);
        }
        lq.orderByAsc(DictionaryArea::getAreaId);
        return getBaseMapper().selectList(lq);
    }

    @Override
    public String queryById(Integer areaId) {
        DictionaryArea obj = getById(areaId);
        if (obj != null) {
            return obj.getAreaName();
        } else {
            return null;
        }
    }

}